﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace duanzhi.web.Areas.m.Controllers
{
    using Models;
    public class tagController : Controller
    {
        //
        // GET: /m/tag/

        public ActionResult Index()
        {
            DataSession<MongoDataContext, tag> db = new DataSession<MongoDataContext, tag>();
            ViewData.Model = db.query;
            return View();
        }
        public ActionResult edit(string id,FormCollection fc) {

            DataSession<MongoDataContext, tag> db = new DataSession<MongoDataContext, tag>();
            if (Request.RequestType == "GET") {
                if (string.IsNullOrWhiteSpace(id))
                {
                    return View();
                }
                else {
                    return View(db.GetById(id.toOId()));
                }
            }


            if (string.IsNullOrWhiteSpace(fc["name"])) { return View(); }
            

            if (string.IsNullOrWhiteSpace(id))//新建
            {
                var t = new tag() {index=0, name = fc["name"] };
                
                db.Add(t);
            }
            else {//修改
                var t = db.GetById(id.toOId());
                TryUpdateModel(t);
                db.Save(t);
                

            }
            return RedirectToAction("index");
        }

        public ActionResult del(string id) { 
            DataSession<MongoDataContext, tag> db = new DataSession<MongoDataContext, tag>();
            db.Delete(id.toOId());
            return RedirectToAction("index");
        }
    }
}
